package srzy

package object rb {

  val TRANS_SUCCESSED: String = "0000"

  // VERSION
  val ONLINE_MSG_TYPE: Int = 2
  val OFFLINE_MSG_TYPE: Int = 1

  // MSGCODE

  val CODE_ECR: Int = 1 // authentication request
  val CODE_ECA: Int = 2 // authentication answer
  val CODE_KAR: Int = 30 // heart request
  val CODE_KAA: Int = 31 // heart reply

  // MSG KEYWORD

  val PARA_SESSION_ID: String = "000"
  val PARA_ERROR_CODE: String = "003"
  val PARA_MSGTYPE: String = "100"

  val patternSessionId = ("(?<=\\[" + PARA_SESSION_ID + "=)[\\p{Graph}&&[^\\]]]*(?=\\])").r
  val patternMsgType = ("(?<=\\[" + PARA_MSGTYPE + "=)[\\p{Graph}&&[^\\]]]*(?=\\])").r
  val patternErrorCode = ("(?<=\\[" + PARA_ERROR_CODE + "=)[\\p{Graph}&&[^\\]]]*(?=\\])").r

  def filterBrace(s: String) = s.replaceAll("""[\{\}]""", "")

  def getSessionId(msg: String) = patternSessionId.findFirstIn(filterBrace(msg)) match {
    case Some(v) => v
    case None => throw new Exception("SessionId not found in msg!")
  }

  def getMsgType(msg: String) = patternMsgType.findFirstIn(filterBrace(msg)) match {
    case Some(v) => Integer.parseInt(v)
    case None => throw new Exception("MsgType not found in msg!")
  }

  def getErrorCode(msg: String) = patternErrorCode.findFirstIn(filterBrace(msg)) match {
    case Some(v) => v
    case None => throw new Exception("ErrorCode not found in msg!")
  }

}